﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models.System.Dtos.DbBackup;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 数据备份
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.21
    /// </summary>
    [Route("api/System/[controller]")]
    public class DbBackupController : BaseApiController
    {
        private DbBackupBll dbBackupBll = new DbBackupBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList([FromQuery] PageInput input)
        {
            var data = await dbBackupBll.GetList(input);
            var output = new PageResult<DbBackupListOutput>
            {
                list = data.list.Select(x => new DbBackupListOutput()
                {
                    id = x.Id,
                    fileName = x.FileName,
                    fileSize = x.FileSize,
                    fileUrl = x.FilePath,
                    creatorTime = x.CreatorTime
                }).ToList(),
                pagination = data.pagination
            };
            return Success(output);
        }
        #endregion

        #region POST
        /// <summary>
        /// 创建备份
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create()
        {
            await dbBackupBll.DbBackup();
            return Success("备份成功");
        }

        [HttpPost("test")]
        [AllowAnonymous]
        public async Task<ResponseResult> CreateTets()
        {
            await dbBackupBll.Creat();
            return Success("备份成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await dbBackupBll.GetInfo(id);
            if (entity != null)
            {
                await dbBackupBll.Delete(entity);
            }
            return Success("删除成功");
        }
        #endregion
    }
}
